<?php

declare(strict_types=1);

namespace App\Job;

use App\Model\CashLog;
use App\Model\Distributors;
use App\Model\Normal;
use App\Model\Order;
use Hyperf\AsyncQueue\Job;
use Hyperf\Di\Annotation\Inject;
use App\Services\AdapayService;

class CashJob extends Job
{
    public int $distributor_id;
    public string $type;
    /**
     * 任务执行失败后的重试次数，即最大执行次数为 $maxAttempts+1 次
     */
    protected int $maxAttempts = 2;
    #[inject]
    protected AdapayService $adapayService;

    public function __construct($distributor_id,$type)
    {
        $this->type = $type;
        $this->distributor_id = $distributor_id;
    }

    public function handle()
    {
        $distributor = Distributors::where('id',$this->distributor_id)
            ->first();
        try{
            $params = array(
                'app_id'=> $this->adapayService->appId,
                'order_no'=> "TX_".$distributor->id. date("YmdHis").rand(1000, 9999),
                'trans_amt'=> $distributor->balance,
                'out_member_id'=> '0',
                'in_member_id' => $this->adapayService->prefix.$distributor->id,
                'goods_desc'=>$distributor->name.'['.$distributor->settlement_type.']取现'.$distributor->balance,
                'goods_title'=>$distributor->name.'['.$distributor->settlement_type.']取现'.$distributor->balance
            );
            $result = $this->adapayService->accountTransfer($params);
            if($result['code'] != 200){
                return '';
            }
            $save['type'] = $this->type;
            $save['cash_no'] ="CS_". $distributor->id .date("YmdHis").rand(1000, 9999);
            $save['cash_amt'] = $distributor->balance;
            $save['distributor_id'] = $distributor->id;
            $save['bank_name'] = $distributor->withdraw_account_bank_name;
            $save['bank_no']  = $distributor->withdraw_account_bank_card_no;
            $save['cert_name'] = $distributor->withdraw_account_bank_card_username;
            $cashLog = CashLog::create($save);
            $params = array(
                'order_no'=>  $save['cash_no'],
                'app_id'=>  $this->adapayService->appId,
                'cash_type'=> 'DM',
                'cash_amt'=> $distributor->balance,
                'member_id'=> $this->adapayService->prefix.$distributor->id,
                'notify_url'=> 'http://ylc.yimitongxun.com/distributors/cash_notify'
            );
            $result = $this->adapayService->drawcash($params);
            if($result['code'] != 200){
                $cashLog->status = CashLog::COMMIT_FALSE;
                $cashLog->error_code = $result['code'];
                $cashLog->error_msg =$result['data']['error_msg'];
                $cashLog->save();
                return '';
            }
            $distributor->balance = 0.00;
            $distributor->save();
        }catch (\Exception $e){
            Normal::logger($distributor->name.'自动提现失败'  . '--'.$e->getMessage(), 'order-queue', 'error');
        }
    }
}
